package com.apptravel.web.controller.admin;

import com.apptravel.dao.AirPortDAO;
import com.apptravel.dao.Page;
import com.apptravel.entity.AirPort;
import com.apptravel.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.apptravel.util.Constants;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * �����Controller
 * <p/>
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2012-1-15
 * Time: 8:26:33
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/admin/airPort")
public class AdminAirPortController extends AdminBaseController {

    private static final String EDIT = "/edit";

    private static final String ADD = "/add";

    private static final String UPDATE = "/update";

    private static final String DEL = "/del";

    private static final String HOME_VIEW = "admin/airPort/home";

    private static final String EDIT_VIEW = "admin/airPort/edit";

    private static final String REDIRECT_VIEW = "/admin/airPort";

    @Autowired
    private AirPortDAO airPortDao;

    /**
     * 列表
     *
     * @param model
     * @return
     */
    @RequestMapping()
    public String home(ModelMap model,String name,Integer page,Integer rows) {
        Map<String,Object> params = new HashMap<String,Object>();
        if(name != null && name.length() > 0){
            params.put("name","%"+name+"%");
        }
        int pageNo = Constants.DEFAULT_PAGE;
		if(page != null && page > 0)
			pageNo = page;
		int pageSize = Constants.DEFAULT_PAGE_SIZE;
		if(rows != null && rows > 0)
			pageSize = rows;
        Page<AirPort> list = airPortDao.getList(params,pageNo,pageSize);
        model.put("page",list);
        return HOME_VIEW;
    }

    /**
     * 编辑
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(EDIT)
    public String edit(ModelMap model, Long id) {
        if (id != null && id > 0) {
            AirPort obj = airPortDao.selectById(id);
            model.put("model", obj);
        }
        return EDIT_VIEW;
    }

    /**
     * 添加
     *
     * @param airPort
     * @param model
     * @return
     */
    @RequestMapping(ADD)
    public String add(AirPort airPort, ModelMap model, HttpServletResponse response) {
        airPortDao.insert(airPort);
        String html = this.makeResponseHtml(0, "");
        JsonUtil.renderHtml(html, response);
        return null;
    }

    /**
     * 更新
     *
     * @param airPort
     * @param model
     * @param response
     * @return
     */
    @RequestMapping(UPDATE)
    public String update(AirPort airPort, ModelMap model, HttpServletResponse response) {
        airPortDao.updateById(airPort);
        String html = this.makeResponseHtml(0, "");
        JsonUtil.renderHtml(html, response);
        return null;
    }

    /**
     * 删除
     *
     * @param ids
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(DEL)
    public String del(String ids,ModelMap model, HttpServletRequest request,HttpServletResponse response){
        String[] idArray = ids.split(",");
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("list", idArray);
        airPortDao.deleteByMap(params);
        String url = request.getContextPath()+REDIRECT_VIEW;
		String html = this.makeResponseHtml(1, url);
		JsonUtil.renderHtml(html, response);
        return null;
    }
}
